package com.example.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import com.example.callback.onSmsReceiverListenner;

public class SmsReceiver extends BroadcastReceiver {

	private onSmsReceiverListenner listener;

	public SmsReceiver(onSmsReceiverListenner listener) {
		this.listener = listener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// Lay du lieu tu intent
		Bundle extract = intent.getExtras();
		Log.i("vao day chua", "vao chua");
		if (extract != null) {
			// Lay thong tin lien quan den SMS theo key pdus
			Object[] smsExtract = (Object[]) extract.get("pdus");
			// Co the co nhieu tin nhan den, nen phai tao mang de luu tru
			for (int i = 0; i < smsExtract.length; i++) {
				// Chuyen tung tin nhan ve mang byte
				byte[] pdu = (byte[]) smsExtract[i];
				// Tao 1 doi dung SmsMessage
				SmsMessage sms = SmsMessage.createFromPdu(pdu);
				// Lay message
				String message = sms.getDisplayMessageBody();
				String address = sms.getDisplayOriginatingAddress();
				Log.i("b_mes", message);
				Log.i("b_add", address);
				listener.onSmsReceiver(address, message);
			}
		}
	}
}
